<?php

use yii\helpers\Url;

$this->title = '功能点管理';

?>

<div class="main-title">
	<div class="title-txt"><?= $this->title ?></div>
</div>
<!--list-->
<div class="panel panel-frame">
    <div class="table-responsive">
        <table class="table table-bordered table-hover">
            <thead>
            <tr>
                <th>名称</th>
                <th>描述</th>
                <th>控制器</th>
                <th>动作</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
                <tr>
                    <td style="text-align: left;">根功能点</td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td class="table-text-align-left">
                        <a class="btn btn-link" onclick="onAddChildFunPoint('0')">添加子功能点</a>
                    </td>
                </tr>
                <?php foreach ($funpoint_list as $funpoint): ?>
                <tr>
                    <td style="text-align: left;padding-left: <?= $funpoint['level'] * 35 ?>px">
                    	<i class="tier-icon"></i>
                    	<?= $funpoint['name'] ?>
                    </td>
                    <td><?= $funpoint['description'] ?></td>
                    <td><?= $funpoint['controller'] ?></td>
                    <td><?= $funpoint['action'] ?></td>
                    <td class="table-text-align-left">
                        <a class="btn btn-link" onclick="onAddFunPoint('<?= $funpoint['id'] ?>')">编辑</a>
                        <a class="btn btn-link" onclick="onDeleteFunPoint('<?= $funpoint['id'] ?>')">删除</a>
                        <a class="btn btn-link" onclick="onAddChildFunPoint('<?= $funpoint['id'] ?>')">添加子功能点</a>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<!-- end list -->
<!-- modal -->
<!-- add function point -->
<div class="modal fade" id="modalAddFunPoint" tabindex="-1" role="dialog" aria-labelledby="modalAddFunPointLabel">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="modalAddFunPointLabel">编辑功能点</h4>
            </div>
            <div id="modalAddFunPointBody" class="modal-body">
            </div>
            <div class="modal-footer">
                <a class="btn btn-primary" onclick="saveFunPoint()"><i class="fa fa-save"></i> 保存
                </a>
                <a class="btn btn-cancel" data-dismiss="modal"><i class="fa fa-remove"></i> 取消
                </a>
            </div>
        </div>
    </div>
</div>
<!-- end add function point -->
<!-- end modal -->
<script>
	function onAddFunPoint(id)
	{
		var modal = $('#modalAddFunPoint');
		modal.modal();
		modal.find('#modalAddFunPointBody').html('正在加载数据');
		requestHtml('<?= Url::toRoute("add-function-point") ?>', {id : id}, true, function (res) {
			modal.find('#modalAddFunPointBody').html(res);
		});
	}

	function onDeleteFunPoint(id)
	{
		confirmDialog(id, '', function (id) {
			requestJson('<?= Url::toRoute("delete-function-point") ?>', {id: id}, true, function (res) {
				if (res.status == 0) {
					history.go(0);
				} else {
					errorMessage(res.message);
				}
			})
		})
	}

	function onAddChildFunPoint(parentId)
	{
		var modal = $('#modalAddFunPoint');
		modal.modal();
		modal.find('#modalAddFunPointBody').html('正在加载数据');
		requestHtml('<?= Url::toRoute("add-function-point") ?>', {parent_id : parentId}, true, function (res) {
			modal.find('#modalAddFunPointBody').html(res);
		});
	}

	function saveFunPoint()
	{
		var form = $('#formAddFunctionPoint');
		var name = form.find('#functionpoint-name').val();
		var controller = form.find('#functionpoint-controller').val();
		var action = form.find('#functionpoint-action').val();

		var mess = '';
		var pattern = /^([a-z_A-Z0-9]|-)*$/;
		if (!name || name.length > 255) {
			mess = '名称不能为空且不能多于255个字符'; console.log(name);console.log(form);
		} else if (!pattern.test(controller)) {
			mess = '控制器只能由英文数字短横线和下划线组成';
		} else if (!pattern.test(action)) {
			mess = '动作只能由英文数字短横线和下划线组成';
		}
		if (mess) {
			warningDialog(mess);
			return;
		}

		requestJson('<?= Url::toRoute("save-function-point") ?>', form.serialize(), true, function (res) {
			if (res.status == 0) {
				history.go(0);
			} else {
				errorMessage(res.message);
			}
		});
	}
</script>